﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI
{
    public partial class CustomerRequest : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            gridRequests.OnClientClick = "loadByRequestID();";
            gridRequests.OnNeedDataSource = gridRequests_OnNeedDataSource;

            if (!IsPostBack)
            {
                SearchFilter = View_CustomerRequests.ColumnNames.BranchID + "=" + Branches.CurrentBranchID.ToString();
                gridRequests.Refresh();
                gridRequests.Columns[0].Visible = false;

            }
        }

        string SearchFilter
        {
            get { return ViewState["SearchFilter"] == null ? "" : ViewState["SearchFilter"].ToString(); }
            set { ViewState["SearchFilter"] = value; }
        }

        protected void btnSearch_OnClick(object sender, EventArgs e)
        {
            SearchFilter = string.Format("(" + View_CustomerRequests.ColumnNames.Requestor + " like '%{0}%' or " +
                View_CustomerRequests.ColumnNames.Title + " like '%{0}%') and " + View_CustomerRequests.ColumnNames.BranchID + "=" + Branches.CurrentBranchID.ToString()
                , txtSearch.Text);
            gridRequests.Refresh();
            updateList.Update();
        }

        protected void btnImportant_OnClick(object sender, EventArgs e)
        {
            if (hidRequestID.Value != "")
            {
                CustomerRequests r = new CustomerRequests();
                r.LoadByPrimaryKey((int.Parse(hidRequestID.Value)));
                if (r.RowCount > 0)
                {
                    r.Important = !r.Important;
                    r.Save();
                    gridRequests.Refresh();
                    updateList.Update();
                }
            }
        }

        protected void btnUnread_OnClick(object sender, EventArgs e)
        {
            if (hidRequestID.Value != "")
            {
                CustomerRequests r = new CustomerRequests();
                r.LoadByPrimaryKey((int.Parse(hidRequestID.Value)));
                if (r.RowCount > 0)
                {
                    r.Unread = !r.Unread;
                    r.Save();

                    gridRequests.Refresh();
                    updateList.Update();
                }
            }
        }

        protected void gridRequests_OnNeedDataSource(object sender, EventArgs e)
        {
            View_CustomerRequests requests = new View_CustomerRequests();
            string sortExpression = ddlSort.SelectedValue + ddlSortDirection.SelectedValue;
            string cols = string.Format("{0},{1},{2},{3},{4},{5},{6}", View_CustomerRequests.ColumnNames.ID, View_CustomerRequests.ColumnNames.Title,
                 View_CustomerRequests.ColumnNames.Requestor, View_CustomerRequests.ColumnNames.CreatedDate, View_CustomerRequests.ColumnNames.AttachedFiles,
                 View_CustomerRequests.ColumnNames.Important, View_CustomerRequests.ColumnNames.Unread);
            int totalRow = requests.Search(SearchFilter, cols, sortExpression, gridRequests.CurrentPageIndex * gridRequests.PageSize, gridRequests.PageSize);
            gridRequests.VirtualItemCount = totalRow;
            gridRequests.DataSource = requests.DefaultView;
        }

        protected void ddlSort_Changed(object sender, EventArgs e)
        {
            gridRequests.Refresh();
            updateList.Update();
        }

        protected void btnLoadByRequestID_OnClick(object sender, EventArgs e)
        {
            if(hidRequestID.Value != "")
            {
                divContent.Visible = true;
                requestContent.LoadByID(int.Parse(hidRequestID.Value));
            }
        }
    }
}